class <%= class_name %>RevisionsController < ApplicationController
  
  before_filter :login_required, :only => [:new, :edit, :update, :create]
  before_filter :check_administrator_role, :only => [:destroy]
  
  # GET /<%= file_name %>_revisions
  # GET /<%= file_name %>_revisions.xml
  def index
    
    @<%= file_name %>_page = <%= class_name %>Page.find_by_title(params[:id].gsub('_',' '))
    @<%= file_name %>_revisions = @<%= file_name %>_page.revisions

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @<%= file_name %>_revisions }
    end
  end

  # GET /<%= file_name %>_revisions/1
  # GET /<%= file_name %>_revisions/1.xml
  def show
    @<%= file_name %>_page = <%= class_name %>Page.find_by_title(params[:<%= file_name %>_id].gsub('_',' '))

    @<%= file_name %>_revision = @<%= file_name %>_page.revisions[0-params[:id].to_i]

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @<%= file_name %>_revision }
    end
  end

  # GET /<%= file_name %>_revisions/new
  # GET /<%= file_name %>_revisions/new.xml
  def new
    @<%= file_name %>_revision = <%= class_name %>Revision.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @<%= file_name %>_revision }
    end
  end

  # GET /<%= file_name %>_revisions/1/edit
  def edit
    @<%= file_name %>_revision = <%= class_name %>Revision.find(params[:id])
  end

  # POST /<%= file_name %>_revisions
  # POST /<%= file_name %>_revisions.xml
  def create
    @<%= file_name %>_revision = <%= class_name %>Revision.new(params[:<%= file_name %>_revision])

    respond_to do |format|
      if @<%= file_name %>_revision.save
        #flash[:notice] = '<%= class_name %>Revision was successfully created.'
        format.html { redirect_to(@<%= file_name %>_revision) }
        format.xml  { render :xml => @<%= file_name %>_revision, :status => :created, :location => @<%= file_name %>_revision }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @<%= file_name %>_revision.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /<%= file_name %>_revisions/1
  # PUT /<%= file_name %>_revisions/1.xml
  def update
    <%= file_name %>_page = <%= class_name %>Page.find_by_title(params[:<%= file_name %>_id].gsub('_',' '))
    <%= file_name %>_revision = <%= file_name %>_page.revisions[0-params[:id].to_i]

    # create a new revision that has the same body as the rollback version
    new_rev = <%= class_name %>Revision.new(:user_id => current_user.id, 
                     :body => <%= file_name %>_revision.body, 
                     :<%= file_name %>_page_id => <%= file_name %>_revision.<%= file_name %>_page_id,
                     :edit_summary => "Rollback to Version #{params[:id]}")

    respond_to do |format|
      if new_rev.save and <%= file_name %>_page.save
        flash[:notice] = "Rolledback to Version #{params[:id]}"
        format.html { redirect_to(<%= file_name %>_path(<%= file_name %>_page.title)) }
        format.xml  { head :ok }
      else
        # TODO: need to redirect if fails
        format.html { redirect_to  <%= file_name %>_history_path(<%= file_name %>_page.title) }
        format.xml  { render :xml => @<%= file_name %>_revision.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /<%= file_name %>_revisions/1
  # DELETE /<%= file_name %>_revisions/1.xml
  def destroy
    @<%= file_name %>_revision = <%= class_name %>Revision.find(params[:id])
    @<%= file_name %>_revision.destroy

    respond_to do |format|
      format.html { redirect_to(<%= file_name %>_revisions_url) }
      format.xml  { head :ok }
    end
  end
end
